<?php

namespace App\Kuafu\WaiMai\Command;

use App\Kuafu\Mybank\Service\MybankService;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\WaiMai\Model\WaiMaiCallbackLogModel;
use App\Kuafu\WaiMai\Model\WaiMaiOrderDetailModel;
use App\Kuafu\WaiMai\Model\WaiMaiOrderIdShopIdMappingModel;
use App\Kuafu\WaiMai\Model\WaiMaiOrderModel;
use App\Kuafu\WaiMai\Model\WaiMaiShopMappingModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class WaiMaiCallbackToQa extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'waimai-callback-to-qa {shopId} {orderId}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步生产callback_logs到测试';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        if (!(config('app.env') == 'dev' || config('app.env') == 'qa')) {
            return false;
        }

        $shopId  = $this->argument('shopId');
        $orderId = $this->argument('orderId');

        if (!$shopId || !$orderId) {
            dd('shopId/orderId不能为空');
        }

        $this->info('同步 生产 至 QA 数据库...开始');
        Config::set('database.connections.rms-prod-wm', [
            'driver'         => 'mysql',
            'url'            => '',
            'host'           => 'rm-bp1981egtkilp75bcso.mysql.rds.aliyuncs.com',
            'database'       => 'kf_waimai_order_1',
            'username'       => 'user_xm',
            'password'       => 'd7fb!sGod!',
            'port'           => '3306',
            'unix_socket'    => '',
            'charset'        => 'utf8mb4',
            'collation'      => 'utf8mb4_general_ci',
            'prefix'         => '',
            'prefix_indexes' => true,
            'strict'         => false,
            'engine'         => null,
            'options'        => extension_loaded('pdo_mysql') ? array_filter([
                \PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ]);
        Config::set('database.connections.rms-prod-wm-all', [
            'driver'         => 'mysql',
            'url'            => '',
            'host'           => 'rm-bp1981egtkilp75bcso.mysql.rds.aliyuncs.com',
            'database'       => 'kf_waimai_order_all',
            'username'       => 'user_xm',
            'password'       => 'd7fb!sGod!',
            'port'           => '3306',
            'unix_socket'    => '',
            'charset'        => 'utf8mb4',
            'collation'      => 'utf8mb4_general_ci',
            'prefix'         => '',
            'prefix_indexes' => true,
            'strict'         => false,
            'engine'         => null,
            'options'        => extension_loaded('pdo_mysql') ? array_filter([
                \PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ]);

        $tableNum     = str_pad((int)$shopId % 100, 3, 0, 0);
        $logTableName = 'kf_waimai_callback_logs_' . $tableNum;
        $orderList    = DB::connection('rms-prod-wm')
                          ->table($logTableName)
                          ->where('shopId', $shopId)
                          ->where('orderId', $orderId)
                          ->get()
                          ->toJson();
        $orderList = collect(json_decode($orderList, true))->map(function ($item) {
            unset($item['id']);
            return $item;
        })->toArray();

        dump($orderList);

        if (empty($orderList)) {
            dd('不存在的订单');
        }

        // 同步 order 表数据
        WaiMaiCallbackLogModel::suffix($shopId)
                              ->where('orderId', $orderId)
                              ->delete();

        WaiMaiCallbackLogModel::suffix($shopId)
                              ->insert($orderList);

        // 同步 waimai_all
        $orderShopIdMap = DB::connection('rms-prod-wm-all')
                            ->table('kf_waimai_shop_mappings')
                            ->where('shopId', $shopId)
                            ->get()
                            ->toJson();

        $orderShopIdMap = collect(json_decode($orderShopIdMap, true))->map(function ($item) {
            unset($item['id']);
            return $item;
        })->toArray();

        WaiMaiShopMappingModel::query()
                              ->where('shopId', $shopId)
                              ->delete();

        WaiMaiShopMappingModel::query()
                              ->insert($orderShopIdMap);


        $this->info('完成');
    }

}
